package com.printeverywhere.action;

import java.io.File;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.interceptor.ServletRequestAware;

import com.printeverywhere.delegator.CommonFunctionDelegator;
import com.printeverywhere.delegator.LocationCategoriesServiceDelegator;
import com.printeverywhere.delegator.TranslationSystemServiceDelegator;
import com.printeverywhere.dto.Items;
import com.printeverywhere.dto.Languages;
import com.printeverywhere.dto.LocationCategories;
import com.printeverywhere.dto.Options;
import com.printeverywhere.dto.ShippingOptions;
import com.printeverywhere.util.AdminConstants;
import com.printeverywhere.util.SearchCriteria;
import com.printeverywhere.util.SearchListCriteria;

public class SellAction extends FrontBaseSupport implements ServletRequestAware {

	public SellAction() {
		super(false, false);
	}

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private List<Languages> languageList;

	private List<LocationCategories> regionList = getRegionListByLevel();

	private List<ShippingOptions> shippingTimeList = getShippingOptionsById(1);

	private List<ShippingOptions> shippingCostChargesList = getShippingOptionsById(2);

	private List<ShippingOptions> shippingInsuranceList = getShippingOptionsById(3);

	private List<ShippingOptions> returnPeriodList = getShippingOptionsById(4);

	private List<ShippingOptions> refundMethodList = getShippingOptionsById(5);

	private List<ShippingOptions> shipToList = getShippingOptionsById(6);

	private Items items;

	private HttpServletRequest request;

	private HttpServletResponse response;

	private File[] file;
	private String myfileContentType;
	private String myfileFileName;

	public List<ShippingOptions> getShippingOptionsById(int id) {
		SearchCriteria searchCriteria = new SearchCriteria();

		searchCriteria.setAttribute("shippingOptionGroups.id", id);
		return (List<ShippingOptions>) CommonFunctionDelegator.getObjectBySearchCriteria(searchCriteria, ShippingOptions.class);
	}

	/**
	 * @return the file
	 */
	public File[] getFile() {
		return file;
	}

	/**
	 * @param file the file to set
	 */
	public void setFile(File[] file) {
		this.file = file;
	}

	/**
	 * @return the request
	 */
	public HttpServletRequest getRequest() {
		return request;
	}

	/**
	 * @param request the request to set
	 */
	public void setRequest(HttpServletRequest request) {
		this.request = request;
	}

	public String showAuctions() throws Exception{
		languageList = TranslationSystemServiceDelegator.getList();
		return SUCCESS;
	}

	public List<LocationCategories> getRegionListByLevel() {
		SearchListCriteria searchListCriteria = new SearchListCriteria();

		searchListCriteria.setAttribute(AdminConstants.LEVEL, new Object[] {
				AdminConstants.ONE_NUMBER, SearchListCriteria.EQ });
		return LocationCategoriesServiceDelegator.getList(searchListCriteria);
	}

	public String saveUpdateAutionItem() throws Exception{
		for (File path : file) {

//			String contentType = request.getContentType();
//  			System.out.println(contentType);
//  			InputStream is = request.getInputStream();
//
//  			int formDataLength = request.getContentLength();
//
//			FileUtil.fileUpload(contentType, is, formDataLength );
		//}
		/*if(items.getTitle() != null){
			this.items.setUserId(SessionUtil.getUser(request).getId());
			AuctionServiceDelegator.saveAuctionSell(items);
			return SUCCESS;
		}else{
			addActionError("Insert Valida Information required by Auction");
			return INPUT;
		}*/
//			String fullFileName = "c:/upload/myfile.txt";
//
//			File theFile = new File(fullFileName);
//
//			FileUtils.copyFile(upload, theFile);
//
//			} catch (Exception e) {
//
//			addActionError(e.getMessage());
//
//			return INPUT;
//
//			}
		 try{
              	System.out.println("Inside Upload");
              	File destDir = new  File("c:\\");
                System.out.println("File Name:"+this.getMyfileFileName());
                System.out.println("File ContentType:"+this.getMyfileContentType());

                FileUtils.copyFileToDirectory(path, destDir);
                return SUCCESS; // redirect to welcome page.
            }catch(Exception exp){
                LOG.error("File Upload Error:"+  exp);
                this.addActionError("File Upload Error:"+ exp);
                return INPUT;
            }
		}
		return SUCCESS;
}


	/**
	 * @return the languageList
	 */
	public List<Languages> getLanguageList() {
		return languageList;
	}

	/**
	 * @param languageList the languageList to set
	 */
	public void setLanguageList(List<Languages> languageList) {
		this.languageList = languageList;
	}

	/**
	 * @return the items
	 */
	public Items getItems() {
		return items;
	}

	/**
	 * @param items the items to set
	 */
	public void setItems(Items items) {
		this.items = items;
	}

	public void setServletRequest(HttpServletRequest arg0) {
		request = arg0;

	}

	/**
	 * @return the response
	 */
	public HttpServletResponse getResponse() {
		return response;
	}

	/**
	 * @param response the response to set
	 */
	public void setResponse(HttpServletResponse response) {
		this.response = response;
	}

	public String getMyfileContentType() {
		return myfileContentType;
	}

	public void setMyfileContentType(String myfileContentType) {
		this.myfileContentType = myfileContentType;
	}

	public String getMyfileFileName() {
		return myfileFileName;
	}

	public void setMyfileFileName(String myfileFileName) {
		this.myfileFileName = myfileFileName;
	}

	public List<LocationCategories> getRegionList() {
		return regionList;
	}

	public List<ShippingOptions> getShippingTimeList() {
		return shippingTimeList;
	}

	public List<ShippingOptions> getShippingCostChargesList() {
		return shippingCostChargesList;
	}

	public List<ShippingOptions> getShippingInsuranceList() {
		return shippingInsuranceList;
	}

	public List<ShippingOptions> getReturnPeriodList() {
		return returnPeriodList;
	}

	public List<ShippingOptions> getRefundMethodList() {
		return refundMethodList;
	}

	public List<ShippingOptions> getShipToList() {
		return shipToList;
	}
}
